package cn.edu360.day09.service.impl;

import cn.edu360.day09.bean.User;
import cn.edu360.day09.bean.UserResult;
import cn.edu360.day09.dao.UserDao;
import cn.edu360.day09.dao.impl.UserDaoImpl;
import cn.edu360.day09.service.UserService;

/**
 * Created by root on 2018/7/12.
 */
public class UserServiceImpl implements UserService{

    /**
     * 登陆：1：通过用户名，得到用户的所有信息
     * 2：判断密码是否一致
     * @param user
     * @return
     */
    @Override
    public UserResult login(User user) {
        UserResult result = new UserResult();

        UserDao userDao = new UserDaoImpl();
        User userByName = userDao.getUserByName(user.getUserName());
        if(userByName!=null){//查找到数据的时候
            if(user.getPasswd().equals(userByName.getPasswd())){//密码一致
                result.setResult(true);
                result.setDesc("登陆成功");
                return result;
            }else{
                result.setResult(false);
                result.setDesc("密码不匹配");
                return result;
            }
        }else{
            result.setResult(false);
            result.setDesc("用户名不存在！");
            return result;
        }
    }

    /**
     * 1:查看是否存在此用户
     * 2：如果不存在，保存数据
     * @param user
     * @return
     */
    @Override
    public UserResult register(User user) {
        UserResult result = new UserResult();
        UserDao userDao = new UserDaoImpl();
        User userByName = userDao.getUserByName(user.getUserName());
        if(userByName!=null){//已经有人注册过了
            result.setResult(false);
            result.setDesc("用户名被占用！");
        }else{//保存数据
            boolean save = userDao.save(user);
            if(save){
                result.setResult(true);
                result.setDesc("注册成功");
            }else{
                result.setResult(false);
                result.setDesc("未知异常，注册失败");
            }

        }
        return result;
    }

    @Override
    public UserResult updatePasswd(User user) {
        return null;
    }

    @Override
    public UserResult findPasswd(User user) {
        return null;
    }
}
